package org.jeecgframework.web.system.pojo.base;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.StaticValue;


/**
 * @author xiul
 * 2016-6-6 下午5:08:15
 * Description 
 */
public class ZyUpCmThread extends Thread{
	
	private int beginR;
	private int endR;
	private XSSFSheet sheet;
	
	public final static String INSERVICE = "服务中";
	public final static String PUSH = "暂停服务";
	public final static String LEAGUE_WAY = "加盟";
	public final static String DIRECT_WAY = "直营";
	
	private static SystemService systemService = ApplicationContextUtil.getBean("systemService");

	
	public ZyUpCmThread(){}
	
	/**
	 * @param beginRow 开始行数
	 * @param endRow 结束行数
	 * @param sheet sheet页
	 */
	public ZyUpCmThread(int beginRow, int endRow, XSSFSheet sheet){
		this.beginR = beginRow;
		this.endR = endRow;
		this.sheet = sheet;
	}
	
	@Override
	public void run(){
		List<ZyCinemaEntity> result = new ArrayList<ZyCinemaEntity>();
		ZyCinemaEntity zyCinemaEntity;
		TSTerritory tsTerritory = null;
		TSDepart belongOrg = null;
		// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
		List<TSTerritory> tsTerritories = systemService.findByProperty(TSTerritory.class, "territoryLevel", (short)2);
		List<TSDepart> departs = systemService.getList(TSDepart.class);
		for (int r = beginR; r <= endR; r++) {
			XSSFRow row = sheet.getRow(r);
			if(null != row){
				zyCinemaEntity = new ZyCinemaEntity();
				zyCinemaEntity.setCode(ExcelUtil.getCellValue(row.getCell(0)));
				zyCinemaEntity.setName(ExcelUtil.getCellValue(row.getCell(1)));
				zyCinemaEntity.setEname(ExcelUtil.getCellValue(row.getCell(2)));
				zyCinemaEntity.setSpreadName(ExcelUtil.getCellValue(row.getCell(3)));
				zyCinemaEntity.setAddress(ExcelUtil.getCellValue(row.getCell(4)));
				zyCinemaEntity.setIsTms(ExcelUtil.getCellValue(row.getCell(5)).trim().toUpperCase().equals("Y")?"1":"0");
				zyCinemaEntity.setIsNormal(ExcelUtil.getCellValue(row.getCell(6)).trim().toUpperCase().equals("Y")?"1":"0");
				zyCinemaEntity.setExceptionReason(ExcelUtil.getCellValue(row.getCell(7)));
				zyCinemaEntity.setPhone(ExcelUtil.getCellValue(row.getCell(8)));
				zyCinemaEntity.setFax(ExcelUtil.getCellValue(row.getCell(9)));
				for (TSTerritory tst : tsTerritories) {
					if(tst.getTerritoryName().equals(ExcelUtil.getCellValue(row.getCell(10)))){
						tsTerritory = tst;
						break;
					}
				}
				zyCinemaEntity.setProvince(tsTerritory);
				for(TSDepart depart :departs){
					if(depart.getShortname().equals(ExcelUtil.getCellValue(row.getCell(11)))){
						belongOrg = depart;
						break;
					}
				}
				zyCinemaEntity.setBelongOrg(belongOrg);
				zyCinemaEntity.setLatitude(ExcelUtil.getCellValue(row.getCell(12)));
				String business = "";
				switch(ExcelUtil.getCellValue(row.getCell(13))){
				case LEAGUE_WAY : 
					business = StaticValue.DIC_LEAGUE_WAY;
					break;
				case DIRECT_WAY : 
					business = StaticValue.DIC_DIRECT_WAY;
					break;
				default:
					business = "";
				}
				zyCinemaEntity.setBusiness(business);
				zyCinemaEntity.setIsNoc(ExcelUtil.getCellValue(row.getCell(14)).trim().toUpperCase().equals("Y")?"1":"0");
				String serverState = "";
				switch(ExcelUtil.getCellValue(row.getCell(15))){
				case INSERVICE : 
					serverState = StaticValue.DIC_CINEMA_INSERVICE;
					break;
				case PUSH : 
					serverState = StaticValue.DIC_CINEMA_PUSH;
					break;
				default:
					serverState = "";
				}
				zyCinemaEntity.setIsService(serverState);
				zyCinemaEntity.setIsDelete("0");
				result.add(zyCinemaEntity);
			}
		}
		systemService.batchSave(result);
	}

	public int getBeginR() {
		return beginR;
	}
	public void setBeginR(int beginR) {
		this.beginR = beginR;
	}

	public int getEndR() {
		return endR;
	}
	public void setEndR(int endR) {
		this.endR = endR;
	}

	public XSSFSheet getSheet() {
		return sheet;
	}
	public void setSheet(XSSFSheet sheet) {
		this.sheet = sheet;
	}
	
	
	
	
	
}
